// request.js
// 定义 API 基地址
// const baseURL = 'https://center.baiyiyl.com/baiyi';
// const baseURL = 'http://172.16.2.157:9011/baiyi';
const baseURL = 'https://center.baiyiyl.com/baiyi';
// const baseURL = 'http://172.16.2.160:9011/baiyi';
const baseFileURL = 'https://center.baiyiyl.com/byyl/files/'
const basePdfURL = 'https://center.baiyiyl.com/byyl/files/static/pdfjs/web/viewer.html?file=https://center.baiyiyl.com/byyl/files/'

// 获取 token 的函数，这里假设从本地存储中获取
const getToken = () => {
	return uni.getStorageSync('token');
};

// 封装 get 请求
const get = (url, params = {}) => {
	const fullURL = baseURL + url;
	const token = getToken();

	return new Promise((resolve, reject) => {
		uni.request({
			url: fullURL,
			method: 'GET',
			data: params,
			header: {
				'token': token
			},
			success: (res) => {
				if (res.data.code === 0 || res.data.code === 200) {
					resolve(res.data);
				} else {
					reject(res.data);
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

// 封装 post 请求
const post = (url, data = {}) => {
	const fullURL = baseURL + url;
	const token = getToken();

	return new Promise((resolve, reject) => {
		uni.request({
			url: fullURL,
			method: 'POST',
			data,
			header: {
				'token': token
			},
			success: (res) => {
				console.log("res",res)
				if (res.data.code === 0) {
					resolve(res.data);
				} else {
					reject(res.data);
				}
			},
			fail: (err) => {
				console.log("err",err)
				reject(err);
			}
		});
	});
};
 
// 封装 put 请求
const put = (url, data = {}) => {
	const fullURL = baseURL + url;
	const token = getToken();

	return new Promise((resolve, reject) => {
		uni.request({
			url: fullURL,
			method: 'PUT',
			data,
			header: {
				'token': token
			},
			success: (res) => {
				console.log("res",res)
				if (res.data.code === 0) {
					resolve(res.data);
				} else {
					reject(res.data);
				}
			},
			fail: (err) => {
				console.log("err",err)
				reject(err);
			}
		});
	});
};

export {
	get,
	post,
	put,
	baseURL,
	baseFileURL,
	basePdfURL
};